module nazo.process;
import std.process, std.c.stdlib, std.file, std.string, std.conv;
alias std.process.system system;
extern(C){
  int mkstemp(const(char)* temp);
}


version(linux){
  import std.c.linux.linux;
  string exec(string s){
    assert(s.find("'")<0);
    scope int[2] pipes;
    pipe ( pipes );
    system("eval '" ~ s ~ "' >&" ~ .toString(pipes[1]));// FIXME: maybe thread unsafe
//    system("bash -c '" ~ s ~ " >&" ~ .toString(pipes[1])~"'");//dash isn't thread safe
    close ( pipes[1] );
    char buf;
    string rv;
    while (read(pipes[0], &buf, 1) > 0)
      rv~=buf;
    close ( pipes[0] );
    return rv;
  }
}else{
  string exec(string cmd){
    const(char)[] fname = "/tmp/nazo-XXXXXX\0".dup[0..$-1];
    mkstemp(fname.ptr);
    system(to!(string)(cmd ~ " > "~fname));
    return cast(string)read(to!(string)(fname));
  }
}

